package com.tepth.vehiclemaintenance.mvp.work.history;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.adapter.VehicleHistoryAdapter;
import com.tepth.vehiclemaintenance.base.BaseFragment;
import com.tepth.vehiclemaintenance.bean.User;
import com.tepth.vehiclemaintenance.bean.VehicleMaintain;
import com.tepth.vehiclemaintenance.config.SpKey;
import com.tepth.vehiclemaintenance.tools.DateUtils;
import com.tepth.vehiclemaintenance.tools.SPUtils;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by zhangyu on 2017/5/10.
 */

public class HistoryFragment extends BaseFragment implements HistoryContract.View,SwipeRefreshLayout.OnRefreshListener {
    private static final String PARAMETER = "vehicleMaintain";
    private VehicleMaintain mVehicleMaintain;

    public static HistoryFragment newInstance(VehicleMaintain vehicleMaintain) {
        HistoryFragment fragment = new HistoryFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(PARAMETER, vehicleMaintain);
        fragment.setArguments(bundle);
        return fragment;
    }

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private VehicleHistoryAdapter mHistoryAdapter;
    private HistoryContract.Presenter mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mVehicleMaintain = (VehicleMaintain) bundle.getSerializable(PARAMETER);
        }
    }

    @Override
    protected void setPresenter() {
        mPresenter = new HistoryPresenter();
        HistoryContract.Model model = new HistoryModel();
        mPresenter.setVM(this, model);
    }

    @Override
    protected void unbindPresenter() {
        mPresenter.onDestroy();
    }

    @Override
    protected void initView(View view) {
        mHistoryAdapter = new VehicleHistoryAdapter(_mActivity, R.layout.item_vehcle_history
                , new ArrayList<VehicleMaintain>());
        mHistoryAdapter.setEmptyView(R.layout.view_empty, (ViewGroup) mRecyclerView.getParent());
        mHistoryAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return R.id.load_more_loading_view;
            }

            @Override
            protected int getLoadFailViewId() {
                return R.id.load_more_load_fail_view;
            }

            @Override
            protected int getLoadEndViewId() {
                return R.id.load_more_load_end_view;
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerView.setAdapter(mHistoryAdapter);
        mPresenter.getVehicleHistory();
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_history, container, false);
    }

    @Override
    public void onNetworkError(String msg) {
        mHistoryAdapter.setNewData(null);
    }

    @Override
    public void onError(String msg) {
        mHistoryAdapter.setNewData(null);
    }

    @Override
    public void onTokenInvalid(String msg) {
        mHistoryAdapter.setNewData(null);
    }

    @Override
    public String getNumber() {
        return mVehicleMaintain.getVehicleNumber();
    }

    @Override
    public String getAuthKey() {
        User user = (User) SPUtils.getObject(_mActivity, SpKey.KEY_USER_INFO);
        return user == null ? "" : user.getAuthKey();
    }

    @Override
    public String getStartTime() {
        return DateUtils.getDateBeforeYear();
    }

    @Override
    public String getEndTime() {
        return DateUtils.getNowCalendar(0);
    }

    @Override
    public void showList(ArrayList<VehicleMaintain> list) {
        mHistoryAdapter.setNewData(list);
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
    }
}
